<?php

namespace app\shop\controller\product;

use think\Controller;

class Product extends Controller
{
    public function __construct()
    {
        // 继承父类
        parent::__construct();

        $this->TypeModel = model('Product.Type');
        $this->ProductModel = model('Product.Product');
        $this->OrderProductModel = model('Order.Product');

        // 获取系统配置里面的选项
        $this->url = config('site.url') ? config('site.url') : '';
    }

    // 首页数据
    public function home()
    {
        // 商品分类列表
        $typelist = $this->TypeModel->order('weigh asc')->limit(8)->select();

        // 查询新品
        $newlist = $this->ProductModel->order('createtime desc')->limit(4)->select();

        $field = [
            'SUM(pronum)' => 'ordnum',
        ];

        // 查询热销商品
        $hotlist = $this->OrderProductModel
            ->with(['proinfo'])
            ->field($field)
            ->group('proid')
            ->order('ordnum desc')
            ->limit(6)
            ->select();

        // 站点名称
        $sitename = config('site.name');

        // 广告图
        $siteadpic = config('site.adpic');
        // 拼上域名信息
        $siteadpic = trim($siteadpic, '/');
        $siteadpic = $this->url . '/' . $siteadpic;

        // 轮播图
        $sitepiclist = config('site.piclist');

        if ($sitepiclist && is_array($sitepiclist)) {
            foreach ($sitepiclist as &$item) {
                $item = trim($item, '/');
                $item = $this->url . '/' . $item;
            }
        }

        // 组装结果
        $result = [
            'typelist' => $typelist,
            'newlist' => $newlist,
            'hotlist' => $hotlist,
            'sitename' => $sitename,
            'siteadpic' => $siteadpic,
            'sitepiclist' => $sitepiclist,
        ];

        $this->success('返回首页数据', null, $result);
        exit;
    }

    // 分类数据
    public function typelist()
    {
        if($this->request->isAjax())
        {
            $typelist = $this->TypeModel->field('id,name')->select();

            $this->success('返回商品分类', null, $typelist);
        }
    }

    // 商品列表数据
    public function prolist()
    {
        if($this->request->isAjax())
        {
            $typeid = $this->request->param('typeid', 0, 'trim');
            $keyword = $this->request->param('keyword', '', 'trim');
            $orderby = $this->request->param('orderby', 'createtime', 'trim');

            $where = [];

            if($typeid)
            {
                $where['typeid'] = $typeid;
            }

            if(!empty($keyword))
            {
                $where['name'] = ['LIKE', "%$keyword%"];
            }

            $prolist = $this->ProductModel->where($where)->order("$orderby desc")->select();

            $this->success('返回商品列表', null, $prolist);
            exit;
        }
    }

    // 商品信息
    public function proinfo()
    {
        if($this->request->isAjax())
        {
            $proid = $this->request->param('proid', 0, 'trim');

            $product = $this->ProductModel->find($proid);

            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }

            $tel = config('site.tel');

            $result = [
                'product' => $product,
                'tel' => $tel
            ];

            $this->success('返回数据', null, $result);
            exit;
        }
    }

    // 获取商品评论列表
    public function comlist()
    {
        if($this->request->isAjax())
        {
            $proid = $this->request->param('proid', 0, 'trim');
            $page = $this->request->param('page', 1, 'trim');

            $product = $this->ProductModel->find($proid);

            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }

            // 每页显示多少条
            $limit = 8;

            // 偏移量
            $offset = ($page-1)*$limit;

            // 评价列表
            $comlist = $this->OrderProductModel
                ->where(['proid' => $proid])
                ->order('id desc')
                ->limit($offset, $limit)
                ->select();

            $this->success('评价列表', null, $comlist);
            exit;
        }
    }
}
